#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<fcntl.h>
#include<unistd.h> 
#include<pthread.h>

int
main(int argc, char* args[])
{
	int fd = open(args[1], O_RDWR | O_CREAT, 0744);
	if(fd < 0){
		perror("open error");
		exit(1);
	}

	char* buffer;
	buffer = "write by fd in test1\n";

	if(write(fd, buffer, strlen(buffer)) < 0){
		printf("there\n");
		perror("write error");
		exit(1);
	}

	int newfd = dup(fd);

	if(newfd < 0){
		perror("dup error");
		exit(1);
	}

	if(close(fd) < 0){
		perror("close error");
		exit(1);
	}

	
	buffer = "write by newfd in test1\n";
	if(write(newfd, buffer, strlen(buffer)) < 0){
		perror("write error");
		exit(1);
	}

	int fd2 = open(args[2], O_RDWR | O_CREAT, 0744);
	if(fd2 < 0){
		perror("open error");
		exit(1);
	}

	if(dup2(fd2, newfd) < 0){
		perror("dup2 error");
		exit(1);
	}

	if(close(fd2) < 0){
		perror("close error");
		exit(1);
	}

	buffer = "after dup2 write by newfd in test2";
	if(write(newfd, buffer, strlen(buffer)) < 0){
		perror("write error");
		exit(1);
	}

	close(newfd);

	exit(0);
}
